package com.itheima.d3_collection_traverse;

import java.util.ArrayList;
import java.util.Collection;
import java.util.function.Consumer;

/**
 * 目标：Collection集合的遍历方式三：JDK8开始新增的Lambda表达式。
 * <p>
 * <p>
 * 遍历方式：
 * 1. 迭代器
 * 2. 增强for
 * 3.foreach方法（结合Lambda）
 */
public class Demo03foreach {
    public static void main(String[] args) {
        Collection<String> c = new ArrayList<>();
        c.add("赵敏");
        c.add("小昭");
        c.add("殷素素");
        c.add("周芷若");
        System.out.println(c);

        // default void forEach(Consumer<? super T> action):  结合Lambda表达式遍历集合：
        c.forEach(System.out::println);


        Collection<Student> students = new ArrayList<>();
        students.add(new Student("首长", 18));
        students.add(new Student("晓迪", 20));
        System.out.println("students = " + students);

        for (Student student : students) {
            System.out.println("student = " + student);
        }

        //匿名内部类
        students.forEach(new Consumer<Student>() {
            @Override
            public void accept(Student student) {
                System.out.println(student);
            }
        });
        //Lambda
        students.forEach(student -> System.out.println(student));//Lambda
        //方法引用
        students.forEach(System.out::println);//方法引用

    }
}

class Student {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }


    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}










